<?php

class UsersController extends \BaseController {

    public $year = '';
    public $last_month = '';
    public $auth_user;

    public function __construct() {
        $this->year = date("Y");
        $this->last_month = date("m",strtotime("-1 month"));
        $this->auth_user = Auth::user();
    }

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		return User::all();
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$user = new User;
        $code = str_random(60);
        $name = Input::get('name');
        $email = Input::get('email');

        $user->name = $name;
        $user->email = $email;
        $user->phone = Input::get('phone');
        $user->gender = Input::get('gender');
        $user->department = Input::get('department');
        $user->salary = Input::get('salary');
        $user->role = Input::get('role');
        $user->password = Hash::make(Input::get('password'));
        $user->token = $code;
        $user->save();

        if($user) {
            //Send Email
            Mail::send('emails.auth.activate', array('link' => URL::route('account.activate', $code), 'name' => $name), function($message) use ($user) {
                $message->to($user->email, $user->name)->subject('激活你的账户');
            });

            return Response::json(['message' => '注册成功，需要邮箱激活', 'status' => 'ok'], 200);
        }
        return Response::json(['message' => '注册失败', 'status' => 'failed'], 400);
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

    public function signs()
    {
        $year  = Input::get('year', $this->year);
        $month =  Input::get('month', $this->last_month);
        $uid  =  Input::get('uid', $this->auth_user->id);
        $profile_list = array();
        if($this->auth_user->role == 'admin')
            $profile = User::get(array('id','name'));
        elseif($this->auth_user->role == 'manager')
            $profile = User::where('department',$this->auth_user->department)->get(array('id','name'));
        else
            $profile = array();
        foreach ($profile as $value) {
            $profile_list[] = array('uid' => $value->id , 'name' => $value->name);
        }
        $signs = Sign::where(DB::raw('YEAR(date)'), $year)
            ->where(DB::raw('Month(date)'), $month)
            ->where('uid',$uid)
            ->with('users')
            ->remember(10)
            ->get()->toArray();

        $json = array('signs' => $signs, 'profile_list' => $profile_list, 'year' => $year, 'month' => $month, 'uid' => $uid);
        return Response::json($json, 200);
    }

    public function current_user()
    {
        return Auth::user();
    }
}
